clear
 set more off

 
 *****Main text statistical models 
 
 use "Cameras_BOPMS.dta", clear

*******
*********
*for descriptives, these are the units in the study
 gen unit_estudio= .
replace unit_estudio = 1 if unit_type_num == 2 
replace unit_estudio =2 if unit_type_num == 4 
replace unit_estudio =3 if unit_type_num == 5 
replace unit_estudio= 4 if unit_type_num == 16
label define unit_estudio  1."Base/Patrulhamento" 2. "GPP/Visibilidade"  3. "GTPP" 4. "Radio Patrulha" 
label values unit_estudio unit_estudio

*********** Collapse GPP Base and GPP Visibilidade 
gen units =.

replace units = 1 if unit_estudio ==3 
replace units = 2 if unit_estudio ==1 | unit_estudio ==4 | unit_estudio == 2 
replace units = 3 if unit_type_num==16
 label define units 1."GTPPs" 2."GPPs"  3. "Radio Patrulha"
 label values units units

 

*********
*********
****Radio Patrulhas were not part of the study all the time. Identify RP that were part of the study 

gen RP_aux =.
replace RP_aux =1 if unit_name_num==35 & date >= 20636 & date <= 20788
replace RP_aux =1 if unit_name_num==36 & date >= 20636 & date <= 20788

*** generate RP that were treated and not when part of the study

gen treated_RP =.
replace treated_RP=1 if d_treatment ==1 & con_camara ==1 & RP_aux==1
replace treated_RP =0 if (d_treatment ==0 | d_treatment ==1) & con_camara ==0 & RP_aux==1
replace treated_RP = . if d_treatment == . 
label define treated_RP 1. "Treated"  0. "Control"
label values treated_RP treated_RP



 
*****Definition of treatment with RP when they are part of the study if not missing***************
gen treated_cam4 =.
replace treated_cam4=1 if d_treatment ==1 & con_camara ==1 | treated_RP==1
replace treated_cam4 =0 if (d_treatment ==0 | d_treatment ==1) & con_camara ==0 |  treated_RP==0
replace treated_cam4= . if d_treatment == . 
label define treated_cam4 1. "Treated"  0. "Control"
label values treated_cam4 treated_cam4

****Table one descriptives treatment and control units**** 

tab treated_cam4 unit_estudio 

*** Droping 3 GTTP shifts that are not treated and have cameras to keep compliers only 
drop if d_treatment == 0 & con_camara == 1


*** Study periods (30 nov - 29 feb, 1 mar - 15 jul, 16 jul - 30 nov)

gen periodos_estudio = 1      if date >= 20422 & date <= 20513
replace periodos_estudio = 2  if date >= 20514 & date <= 20620
replace periodos_estudio = 3  if date >= 20621 & date <= 20788
tab periodos_estudio

******
*********
    


****** crear Dependent vriables
gen new_stop= tot_bopm_orig_abord + tot_bopm_orig_encon + tot_bopm_ci_sospech + tot_bopm_ci_sosiego + tot_bopm_ci_agresion
gen d_new_stop = new_stop
recode d_new_stop 1=1
recode d_new_stop 2=1 
recode d_new_stop 3=1 

***checar si d_bopm esta bien
gen tot_bopm_rec = tot_bopm_orig_abord+ tot_bopm_orig_oper+ tot_bopm_orig_encon+ tot_bopm_orig_solic+ tot_bopm_ci_sospech+ tot_bopm_ci_sosiego+ tot_bopm_ci_hospit+ tot_bopm_ci_traf_drogas+ tot_bopm_ci_alarma+ tot_bopm_ci_agresion+ tot_bopm_ci_transito+ tot_bopm_ci_mujer

tab tot_bopm_rec if tot_bopm_rec >0
*a total of 1043 bopms

gen tot_bopm_recd =tot_bopm_rec
recode tot_bopm_recd 0=0 1/6=1 

tab tot_bopm_recd 
*520 eventos
 
************Generate supervisors  treated with cameras

gen supervision = sup_treat_cam

label define supervision 0."Out of study" 1."Without Camera"  2. "With Camera"
 label values supervision supervision

 *** label camera usage
 label define con_uso 0."Not Record" 1. "Record"
 label values con_uso con_uso
*****************


********* Crete Modality of treatment

gen t1_who_num = 0 if t1_who == "Control"
replace t1_who_num = 1 if t1_who == "Half" |   t1_who == "Some"
replace t1_who_num = 2 if t1_who == "Full"
tab t1_who_num con_camara
*** Si hay variacion
label define twho_lab 0 "Control" 1 "Some officers" 2 "Full team"
label values t1_who_num twho_lab
tab t1_who t1_who_num

tab t2_mode
gen t2_mode_num = 0 if t2_mode == "Control"
replace t2_mode_num = 1 if t2_mode == "2 interac/ 1 ligada" |   t2_mode == "Interactive" |   t2_mode == "Interactive"
replace t2_mode_num = 2 if t2_mode == "Always on" | t2_mode == "Always on, 6hrs noct"
tab t2_mode t2_mode_num


label define thow_lab 0 "Control" 1 "Interactive" 2 "Always on"
label values t2_mode_num thow_lab
tab t2_mode t2_mode_num

******
****** Tabla descriptiva de BOPMs

tab tot_bopm unit_estudio if tot_bopm >0
tab tot_bopm_recd unit_estudio 
*** y asi para cada uno

****BOPM regresions 

*cd "/Volumes/GoogleDrive/My Drive/BoxMigration/New Brazil Gustavo/Proyecto Camaras/Bea_AJPS/results"
 
*reg total_furtos  b4i.periodos_estudio
*outreg2 using "Turnos_before_after_6t.xls", dec(3) ctitle(Furtos) append
** In interactive models I exlcude PR because there are too few and f in models can't be calculated
logit d_bopm i.treated_cam4  i.units, vce(cluster unit_name_num)  
estimate store r1
logit d_bopm i.units#i.treated_cam4 if   units!=3, vce(cluster unit_name_num)
estimate store r2
logit d_new_stop i.treated_cam4 i.units, vce(cluster unit_name_num) 
estimate store r3
logit d_new_stop  i.units#i.treated_cam4 if  units!=3, vce(cluster unit_name_num)
estimate store r4
logit d_bopm_orig_oper i.treated_cam4 i.units, vce(cluster unit_name_num) 
estimate store r5
logit d_bopm_orig_oper  i.units#i.treated_cam4 if  units!=3, vce(cluster unit_name_num) 
estimate store r6
logit d_bopm_orig_solic i.treated_cam4 i.units, vce(cluster unit_name_num) 
estimate store r7
logit d_bopm_orig_solic  i.units#i.treated_cam4 if units!=3, vce(cluster unit_name_num)  
estimate store r8

estout r1 r2 r3 r4 r5 r6 r7 r8 using "BOPMS_logit.xls", replace ///
cells(b(star fmt(a3)) se(par(`"="("'`")""')fmt(4))) stats(N r2) starlevels(* 0.10 ** 0.05 *** 0.01)

*********
logit d_bopm  i.t1_who_num i.units if periodos_estudio==1 & units!=3, vce(cluster unit_name_num)
outreg2 using "modalidad_reg.xls", dec(3) ctitle(Coverage) replace 
logit d_bopm  i.t1_who_num#i.treated_cam4 i.units if periodos_estudio==1 & units!=3, vce(cluster unit_name_num)
outreg2 using "modalidad_reg.xls", dec(3) ctitle(Coverage) append
logit d_bopm  i.t2_mode_num i.units if (periodos_estudio==1 | periodos_estudio==2) & units!=3, vce(cluster unit_name_num)
outreg2 using "modalidad_reg.xls", dec(3) ctitle(Protocol) append
logit d_bopm  i.t2_mode_num#i.treated_cam4 i.units if  (periodos_estudio==1 | periodos_estudio==2) & units!=3, vce(cluster unit_name_num)
outreg2 using "modalidad_reg.xls", dec(3) ctitle(Protocol) append
logit d_bopm  i.supervision i.units if periodos_estudio ==2 & units!=3, vce(cluster unit_name_num)
outreg2 using "modalidad_reg.xls", dec(3) ctitle(Supervision) append
logit d_bopm  i.supervision#i.treated_cam4 i.units if periodos_estudio ==2 & units!=3, vce(cluster unit_name_num)
outreg2 using "modalidad_reg.xls", dec(3) ctitle(Supervision) append


******************Uso camaras
set more off
logit d_bopm i.treated_cam4 i.con_uso i.units, vce(cluster unit_name_num)
outreg2 using "APSR_uso_reg.tex", dec(3) ctitle(Total BOPMs) replace 
logit d_new_stop i.treated_cam4 i.con_uso i.units, vce(cluster unit_name_num)
outreg2 using "APSR_uso_reg.tex", dec(3) ctitle(Stops) append
logit  d_bopm_orig_oper  i.treated_cam4 i.con_uso i.units, vce(cluster unit_name_num)
outreg2 using "APSR_uso_reg.tex", dec(3) ctitle(Operation) append
logit d_bopm_orig_solic  i.treated_cam4 i.con_uso i.units, vce(cluster unit_name_num)
outreg2 using "APSR_uso_reg.tex", dec(3) ctitle(Requests) append

***** Graphs main text taht are related to cameras (not survey)

****BOPMS

set more off
logit d_bopm i.treated_cam4  i.units, vce(cluster unit_name_num)  
#delimit ;
coefplot, drop(_cons 1.units 2.units 3.units 0.treated_cam )  xline(1)  eform xtitle(Odds ratio) 
xlabel(0(.2)1.6, grid)
title("Total BOPMs")
 
xlabel(, labsize(vsmall))
coeflabels(
1.treated_cam4 = " ", labcolor(black) labsize(small))
mlabel format(%3.2f) mlabposition(12) msize(medium) mlabcolor(black) mcolor(maroon) mlabsize(medium) 
ciopts(lcolor(navy))
graphregion(fcolor(white) lcolor(none) ifcolor(none) ilcolor(none))
plotregion(fcolor(white) ifcolor(none))
name(total4, replace)
;
#delimit cr

set more off
logit d_new_stop i.treated_cam4 i.units, vce(cluster unit_name_num) 
#delimit ;
coefplot, drop(_cons 1.units 2.units 3.units 0.treated_cam ) xline(1) eform xtitle(Odds ratio)
xlabel(0(.2)1.6, grid)
title ("Proactive Policying")
subtitle("Stop-and-search & other encounters")

xlabel(, labsize(vsmall))
coeflabels(
1.treated_cam4 = " ", labcolor(black) labsize(small))
mlabel format(%3.2f) mlabposition(12) msize(medium) mlabcolor(black) mcolor(maroon) mlabsize(medium) 
ciopts(lcolor(navy))
graphregion(fcolor(white) lcolor(none) ifcolor(none) ilcolor(none))
plotregion(fcolor(white) ifcolor(none))
name(stops3, replace)
;
#delimit cr


set more off
logit d_bopm_orig_oper i.treated_cam4 i.units, vce(cluster unit_name_num) 
#delimit ;
coefplot, drop(_cons 1.units 2.units 3.units 0.treated_cam ) xline(1)  eform xtitle(Odds ratio)
xlabel(0(.2)1.6, grid)
title("Reactive Policing")
subtitle ("Operation Center calls & dispatch")


xlabel(, labsize(vsmall))
coeflabels(
1.treated_cam4 = " ", labcolor(black) labsize(small))
mlabel format(%3.2f) mlabposition(12) msize(medium) mlabcolor(black) mcolor(maroon) mlabsize(medium) 
ciopts(lcolor(navy))
graphregion(fcolor(white) lcolor(none) ifcolor(none) ilcolor(none))
plotregion(fcolor(white) ifcolor(none))
name(operation3, replace)
;
#delimit cr



set more off
logit d_bopm_orig_solic i.treated_cam4 i.units, vce(cluster unit_name_num) 
#delimit ;
coefplot, drop(_cons 1.units 2.units 3.units 0.treated_cam ) xline(1)  eform xtitle(Odds ratio)
xlabel(0(.2)1.6, grid)
title ("Reactive policing")
subtitle("Street requests")


xlabel(, labsize(vsmall))
coeflabels(
1.treated_cam4 = " ", labcolor(black) labsize(small))
mlabel format(%3.2f) mlabposition(12) msize(medium) mlabcolor(black) mcolor(maroon) mlabsize(medium) 
ciopts(lcolor(navy))
graphregion(fcolor(white) lcolor(none) ifcolor(none) ilcolor(none))
plotregion(fcolor(white) ifcolor(none))
name(requests3, replace)
;
#delimit cr

******


#delimit ;
graph combine total4 stops3 operation3 requests3 
,
cols(2) rows(2)  commonscheme imargin(0 0 0 0) 
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
name(gcomm, replace)
ycommon
ysize(3) xsize(4)
;


#delimit cr
graph export  "BOPMs.png", replace
graph close	

************

***********Graphs of supervisors 

*sacar Radio Patrulhas pues no son parte del estudio hasta el periodo tres units!=3 
*******
set more off
logit d_bopm i.supervision i.units if   periodos_estudio ==2 & units!=3, vce(cluster unit_name_num)
margins 1.supervision 2.supervision, post

#delimit ;
coefplot, drop(_cons) 
title("Total BOPMs", size(large)) 
xlabel(, grid)  
xlabel( , labsize(small))
baselevels 
coeflabels(1.supervision= "Supervisor without camera" 2.supervision ="Supervisor with camera"
, 
labcolor(black) labsize(medium))
mlabel format(%3.2f) mlabposition(12) msize(large) mlabcolor(black) mcolor(maroon) mlabsize(large) 
ciopts(lcolor(navy))
graphregion(fcolor(white) lcolor(none) ifcolor(none) ilcolor(none))
plotregion(fcolor(white) ifcolor(none))
name(sup, replace)
;
#delimit cr



**********

set more off
logit d_bopm i.treated_cam4#i.supervision i.units if periodos_estudio ==2 & units!=3, vce(cluster unit_name_num) 
margins 0.treated_cam4#1.supervision 0.treated_cam4#2.supervision 1.treated_cam4#1.supervision  1.treated_cam4#2.supervision, post

#delimit ;
coefplot, drop(_cons) 
title("Total BOPMs", size(large))   
xlabel(, grid)  
xlabel( , labsize(small))
baselevels 
coeflabels(0.treated_cam4#1.supervision = "Supervisor NC, Officer NC"  0.treated_cam4#2.supervision = "Supervisor C, Officer NC" 1.treated_cam4#1.supervision //
= "Supervisor NC, Officer C" 1.treated_cam4#2.supervision = "Supervisor C, Officers C" 
, 
labcolor(black) labsize(medium))
mlabel format(%3.2f) mlabposition(12) msize(large) mlabcolor(black) mcolor(maroon) mlabsize(large) 
ciopts(lcolor(navy))
graphregion(fcolor(white) lcolor(none) ifcolor(none) ilcolor(none))
plotregion(fcolor(white) ifcolor(none))
name(sup2, replace)
;

#delimit ;

graph combine sup sup2
,
cols(2) rows(1)   
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
name(gcomm, replace)
ycommon
ysize(4) xsize(8)
;

#delimit cr
graph export  "/Volumes/GoogleDrive/My Drive/BoxMigration/New Brazil Gustavo/Proyecto Camaras/Bea_AJPS/graphs/APSR_supervisor.png", replace
graph close	

****Graphs of camera usage 

set more off
logit d_bopm i.treated_cam4 i.con_uso i.units, vce(cluster unit_name_num)
margins   0.treated_cam4 0.con_uso 1.con_uso, post
#delimit ;
coefplot, drop(_cons 1.units 2.units 3.units) 
title ("Total BOPMs")
xlabel(, grid)
xlabel( , labsize(small))
baselevels 
coeflabels(
, 
labcolor(black) labsize(small))
 mcolor(maroon) mlabsize(small) 
ciopts(lcolor(navy))
graphregion(fcolor(white) lcolor(none) ifcolor(none) ilcolor(none))
plotregion(fcolor(white) ifcolor(none))
name(uso_total, replace)
;
#delimit cr
******

*******
set more off
logit d_new_stop i.treated_cam4 i.con_uso i.units, vce(cluster unit_name_num)
margins   0.treated_cam4 0.con_uso 1.con_uso, post
#delimit ;
coefplot, drop(_cons 1.units 2.units 3.units) 
title ("Proactive Policying")
subtitle("Stop-and-search & other encounters")   
xlabel(, grid)
xlabel( , labsize(small))
baselevels 
coeflabels(
, 
labcolor(black) labsize(small))
 mcolor(maroon) mlabsize(small) 
ciopts(lcolor(navy))
graphregion(fcolor(white) lcolor(none) ifcolor(none) ilcolor(none))
plotregion(fcolor(white) ifcolor(none))
name(uso_stop, replace)
;
#delimit cr
******
set more off
logit  d_bopm_orig_oper  i.treated_cam4 i.con_uso i.units, vce(cluster unit_name_num)
margins   0.treated_cam4 0.con_uso 1.con_uso, post
#delimit ;
coefplot, drop(_cons 1.units 2.units 3.units) 
title ("Reactive Policing")
subtitle("Operation Center (calls and dispatch)")   
xlabel(, grid)
xlabel( , labsize(small))
baselevels 
coeflabels(
, 
labcolor(black) labsize(small))
 mcolor(maroon) mlabsize(small) 
ciopts(lcolor(navy))
graphregion(fcolor(white) lcolor(none) ifcolor(none) ilcolor(none))
plotregion(fcolor(white) ifcolor(none))
name(uso_oper, replace)
;
#delimit cr
******
set more off
logit d_bopm_orig_solic  i.treated_cam4 i.con_uso i.units, vce(cluster unit_name_num)
margins   0.treated_cam4 0.con_uso 1.con_uso, post
#delimit ;
coefplot, drop(_cons 1.units 2.units 3.units) 
title ("Reactive Policing")
subtitle("Street requests")   
xlabel(, grid)
xlabel( , labsize(small))
baselevels 
coeflabels(
, 
labcolor(black) labsize(small))
 mcolor(maroon) mlabsize(small) 
ciopts(lcolor(navy))
graphregion(fcolor(white) lcolor(none) ifcolor(none) ilcolor(none))
plotregion(fcolor(white) ifcolor(none))
name(uso_street, replace)
;
#delimit cr

***combine
#delimit ;
graph combine uso_total uso_stop uso_oper uso_street 
,
cols(2) rows(2)  commonscheme imargin(0 0 0 0) 
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
name(gcomm, replace)
ycommon
ysize(3) xsize(4)
;


#delimit cr
graph export  "/Volumes/GoogleDrive/My Drive/BoxMigration/New Brazil Gustavo/Proyecto Camaras/Bea_AJPS/graphs/APSR_USO.png", replace
graph close	



